﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using JetBrains.Annotations;
using Orchard.ContentManagement.Drivers;
using Orchard;
using Orchard.Localization;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
using System.Diagnostics;
using RaisingStudio.Razor.Services;
using Orchard.Environment.Features;
using RaisingStudio.Razor.ViewModels;
using RaisingStudio.Razor.Utilities;

namespace RaisingStudio.Razor.Drivers
{
    [UsedImplicitly]
    public class RazorFieldDriver: ContentFieldDriver<Fields.RazorField>
    {
        private IRazorService razorService;
        private IOrchardServices orchardServices;
        private IFeatureManager featureManager;

        public RazorFieldDriver(IOrchardServices orchardServices, IFeatureManager featureManager, IRazorService razorService)
        {
            this.orchardServices = orchardServices;
            this.featureManager = featureManager;
            this.razorService = razorService;
        }

        private static string GetPrefix(ContentField field, ContentPart part)
        {
            return (part.PartDefinition.Name + "." + field.Name).Replace(" ", "_");
        }
        
        protected override DriverResult Display(ContentPart part, Fields.RazorField field, string displayType, dynamic shapeHelper)
        {
            return ContentShape("Fields_RazorField",
                    () => shapeHelper.Fields_RazorField(
                        ContentField: field, 
                        Content: this.razorService.Run(field.Template, this.orchardServices, part))
                );
        }

        protected override DriverResult Editor(ContentPart part,
                                               Fields.RazorField field,
                                               dynamic shapeHelper)
        {
            bool useTinyMce = featureManager.GetEnabledFeatures().Any(f => f.Id == "RaisingStudio.Razor.TinyMce");
            string addMediaPath = useTinyMce ? new PathBuilder(part).AddContentType().AddContainerSlug().AddSlug().ToString() : string.Empty;
            RazorEditorViewModel razorEditorViewModel = new ViewModels.RazorEditorViewModel
            {
                Name = field.Name,
                Template = field.Template,
                UseTinyMce = useTinyMce,
                AddMediaPath = addMediaPath
            };
            return ContentShape("Fields_RazorField_Edit",
                () => shapeHelper.EditorTemplate(
                          TemplateName: "Fields/RazorField",
                          Model: razorEditorViewModel,
                          Prefix: GetPrefix(field, part)));
        }

        protected override DriverResult Editor(ContentPart part,
                                               Fields.RazorField field,
                                               IUpdateModel updater,
                                               dynamic shapeHelper)
        {            
            RazorEditorViewModel razorEditorViewModel = new RazorEditorViewModel();
            if (updater.TryUpdateModel(razorEditorViewModel, GetPrefix(field, part), null, null))
            {
                field.Template = razorEditorViewModel.Template;
            }
            return Editor(part, field, shapeHelper);
        }

        protected override void Importing(ContentPart part, Fields.RazorField field,
            ImportContentContext context)
        {
            var importedText = context.Attribute(GetPrefix(field, part), "Template");
            if (importedText != null)
            {
                field.Storage.Set(null, importedText);
            }
        }

        protected override void Exporting(ContentPart part, Fields.RazorField field,
            ExportContentContext context)
        {
            context.Element(GetPrefix(field, part))
                .SetAttributeValue("Template", field.Storage.Get<string>(null));
        }
    }
}